// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Moving Block Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 61 : TYPE_MOVINGBLOCK

// Function declarations
reserve function MovingBlock_DebugDraw
reserve function MovingBlock_DebugSpawn

// Static Values

// Tables

function MovingBlock_DebugDraw
	DrawSprite(0)
end function


function MovingBlock_DebugSpawn
	CreateTempObject(TypeName[Moving Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].priority = PRIORITY_XBOUNDS
	object[tempObjectPos].state = 1
end function


event ObjectMain
	switch object.state
	case 0
		if object[+2].value0 == 1
			object.state++
		end if
		break
	case 1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.state++
				if stage.debugMode == 0
					if currentPlayer == 0
						CallNativeFunction2(SetAchievement, 5, 100)
					end if
				end if
			end if
		next
		break
	case 2
		temp0 = object.xpos
		temp0 &= -0x10000
		object.xpos += 0x10000
		ObjectTileCollision(CSIDE_LWALL, 16, 0, 0)
		if checkResult == 1
			object.state++
		end if
		object.value0 = object.xpos
		object.value0 &= -0x10000
		object.value0 -= temp0
		temp1 = object.xpos
		object.xpos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].xpos += object.value0
			end if
		next
		object.xpos = temp1
		break
	case 3
		temp0 = object.ypos
		temp0 &= -0x10000
		object.ypos += object.yvel
		object.yvel += 0x3800
		ObjectTileCollision(CSIDE_FLOOR, 0, 8, 0)
		if checkResult == 1
			object.state++
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		object.value0 = object.ypos
		object.value0 &= -0x10000
		object.value0 -= temp0
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].ypos += object.value0
			end if
			if object[currentPlayer].gravity == GRAVITY_GROUND
				if checkResult == 4
					CallFunction(PlayerObject_Kill)
				end if
			end if
		next
		object.ypos = temp1
		break
	case 4
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		break
	end switch
end event


event ObjectDraw
	if object.state > 0
		DrawSprite(0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 1, 1)
	foreach (TypeName[Moving Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
	next
	SetTableValue(TypeName[Moving Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(MovingBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(MovingBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
